<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { luckDrawApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading, allNumber } from '@/utils/public';
  const formLabelWidth = '160px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    form_type:0,
    poolIfDefault:[] as Array<any>,
    poolOptions:[] as Array<any>,
  })
  interface formQuert {
    pool_id:number
    min_num:number
    max_num:number
    pool_name:string
    pool_type:number
    if_default:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    pool_name: [{ required: true, message: "请输入礼物池名称", trigger: "blur" }],
    min_num: [{ required: true, message: "请输入参与次数最小值", trigger: "blur" }],
    max_num: [{ required: true, message: "请输入参与次数最大值", trigger: "blur" }],
    pool_type: [{ required: true, message: "请选择礼物池类型", trigger: "blur" }],
    if_default: [{ required: true, message: "请选择是否默认", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const { pool_id } = form
      const loading = Toastloading()
      let submitSet = false
      if(pool_id) {
        await luckDrawApi.wishLuckEditPool(form).then(()=>{
          ToastSuccess('编辑礼物池成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }else {
        await luckDrawApi.wishLuckAddPool(form).then(()=>{
          ToastSuccess('新增礼物池成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        $emit('updateList')
        closeDialog()
      }
    })
  }
  const openDialog = (query:any={})=> {
    const { id, min_num=0, max_num=0, pool_name='', pool_type='', if_default='', form_type=0, poolIfDefault=[], poolOptions=[], pool_level=0 } = query
    if(!pool_level) return ToastWarning('缺少 pool_level')
    action.form_type = form_type
    let formObj = {} as any
    if(form_type == 1) {
      formObj = {
        pool_name,
        min_num,
        max_num,
        pool_type,
        pool_level
      }
    }
    if(form_type == 2) {
      formObj = {
        pool_name,
        pool_type,
        if_default,
        pool_level
      }
    }
    action.poolOptions = poolOptions
    action.poolIfDefault = poolIfDefault
    if(id) formObj.pool_id = id
    Object.assign(form,{
      ...formObj
    })
    action.title = `${id?`编辑`:'新增'}${['','普通','个人'][form_type] || ''}礼物池：${pool_name}`
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    action.form_type = 0
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="40%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" prop="pool_name" label="礼物池名称">
          <el-input v-model="form.pool_name" placeholder="请输入礼物池名称" />
        </el-form-item>
        <template v-if="action.form_type == 1">
          <el-form-item :label-width="formLabelWidth" prop="min_num" label="参与次数最小值">
            <el-input @input="allNumber(form,'min_num')" v-model="form.min_num" placeholder="请输入参与次数最小值" />
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" prop="max_num" label="参与次数最大值">
            <el-input @input="allNumber(form,'max_num')" v-model="form.max_num" placeholder="请输入参与次数最大值" />
          </el-form-item>
        </template>
        <template v-if="action.form_type == 2">
          <el-form-item :label-width="formLabelWidth" prop="pool_type" label="礼物池类型">
            <el-select v-model="form.pool_type" placeholder="请选择礼物池类型">
              <el-option
                v-for="(item, index) in action.poolOptions"
                :key="index"
                :label="item.label"
                :value="item.type"
              />
            </el-select>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" prop="if_default" label="是否默认">
            <el-select v-model="form.if_default" placeholder="请选择是否默认">
              <el-option
                v-for="(item, index) in action.poolIfDefault"
                :key="index"
                :label="item.label"
                :value="item.type"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
